﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.Services
{
    public class MaintainerService
    {
        public ServiceResult UpdateMaintainer(Guid id, string email, string mobile, string name, string startTime, string endTime)
        {
            if (string.IsNullOrEmpty(name)
                || string.IsNullOrEmpty(email) )
            {
                return new ServiceResult(false, ErrorCode.COM_InvalidParameter, ErrorCode.String(ErrorCode.COM_InvalidParameter));
            }

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.Maintainer> dRep = new EntityRepository<Entities.Maintainer>(dbContext);

                Entities.Maintainer em = dRep.Get(p => p.Id == id);

                if (em != null)
                {
                    em.Email = email;
                    em.Name = name;
                    em.Mobile = mobile;
                    em.StartTime = startTime;
                    em.EndTime = endTime;
                    dRep.Update(em);

                    try
                    {
                        dbContext.SaveChanges();
                        return new ServiceResult(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError));
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorToTag(Library.LogTAG, string.Format("更新监控人员:{0}出错,ex:{1}", id, ex));
                        return new ServiceResult(false, ErrorCode.COM_Exception, ErrorCode.String(ErrorCode.COM_Exception));
                    }
                }
                else
                {
                    return new ServiceResult(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                }
            }
        }
    }
}
